/**
 * Static page route — fully driven by the merchant's dashboard.
 *
 * The merchant creates pages in the Brainerce dashboard under
 *   Sell → Content → Page (one row per page, each with its own `slug`)
 *
 * This catch-all renders any PAGE by its `data.slug`. `/pages/about` →
 * fetches PAGE with slug='about'. Mounting under `/pages/` (rather than
 * the root) keeps the route from clashing with existing static routes
 * like /cart, /login, /products.
 *
 * Security: page HTML is sanitized via `sanitizeHtml` before injecting.
 */
import * as React from 'react';
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';

import { getServerClient } from '@/core/lib/brainerce.server';
import { sanitizeHtml } from '@/core/lib/sanitize';
import { decodeSlug } from '@/core/lib/utils';

<% if (i18nEnabled) { %>
type PageProps = {
  params: Promise<{ locale: string; slug: string }>;
};

export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
  const { locale, slug: rawSlug } = await params;
  const slug = decodeSlug(rawSlug);
  const page = await (await getServerClient(locale)).content.page.getBySlug(slug, locale).catch(() => null);
  if (!page) return { title: slug };
  const seo = page.data.seo ?? {};
  return {
    title: seo.title ?? page.data.title,
    description: seo.description,
    openGraph: seo.ogImage ? { images: [{ url: seo.ogImage }] } : undefined,
  };
}

export default async function StaticPage({ params }: PageProps) {
  const { locale, slug: rawSlug } = await params;
  const slug = decodeSlug(rawSlug);
  // getBySlug returns null only on a genuine 404; any other API failure (a
  // 403 from the channel's origin check, a network error) throws and surfaces
  // as the error it is instead of masquerading as a missing page.
  const page = await (await getServerClient(locale)).content.page.getBySlug(slug, locale);
  if (!page) notFound();
  return (
    <article className="mx-auto max-w-3xl px-4 py-10 sm:px-6 lg:px-8">
      <h1 className="text-foreground mb-6 text-2xl font-semibold sm:text-3xl">
        {page.data.title}
      </h1>
      <div
        className="prose prose-sm dark:prose-invert max-w-none"
        dangerouslySetInnerHTML={{ __html: sanitizeHtml(page.data.html) }}
      />
    </article>
  );
}
<% } else { %>
type PageProps = {
  params: Promise<{ slug: string }>;
};

export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
  const { slug: rawSlug } = await params;
  const slug = decodeSlug(rawSlug);
  const page = await (await getServerClient()).content.page.getBySlug(slug).catch(() => null);
  if (!page) return { title: slug };
  const seo = page.data.seo ?? {};
  return {
    title: seo.title ?? page.data.title,
    description: seo.description,
    openGraph: seo.ogImage ? { images: [{ url: seo.ogImage }] } : undefined,
  };
}

export default async function StaticPage({ params }: PageProps) {
  const { slug: rawSlug } = await params;
  const slug = decodeSlug(rawSlug);
  // getBySlug returns null only on a genuine 404; any other API failure (a
  // 403 from the channel's origin check, a network error) throws and surfaces
  // as the error it is instead of masquerading as a missing page.
  const page = await (await getServerClient()).content.page.getBySlug(slug);
  if (!page) notFound();
  return (
    <article className="mx-auto max-w-3xl px-4 py-10 sm:px-6 lg:px-8">
      <h1 className="text-foreground mb-6 text-2xl font-semibold sm:text-3xl">
        {page.data.title}
      </h1>
      <div
        className="prose prose-sm dark:prose-invert max-w-none"
        dangerouslySetInnerHTML={{ __html: sanitizeHtml(page.data.html) }}
      />
    </article>
  );
}
<% } %>
